/**
 * Project Name:springBoot-controller
 * File Name:Io.java
 * Package Name:com.libra.io.controller
 * Date:2018年8月27日下午8:38:13
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.io.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.libra.io.service.IoService;

/**
 * ClassName:文件的上传和下载 <br/>
 * Date: 2018年8月27日 下午8:38:13 <br/>
 * 
 * @author wangzhch
 * @version
 * @since JDK 1.8
 * @see
 */
@RestController
@RequestMapping("io")
public class IoController {
	static Logger log = LoggerFactory.getLogger(IoController.class);

	@Autowired
	private IoService ioService;
	/**
	 * 
	 * testUpload:(上传到项目的跟路径). <br/>
	 * @author wangzhch
	 * @param file
	 * @return
	 * @throws IOException
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/testUploadByHellPath",method = RequestMethod.POST)
	public String testUploadByHellPath(@RequestParam("file") MultipartFile file){
		
		log.info("上传文件类型:{}", file.getContentType());
		log.info("上传文件大小:{}", file.getSize());
		log.info("上传文件名:{}", file.getOriginalFilename());
		try {
			String msg = ioService.testUploadByHellPath(file);
			return msg;
		} catch (IOException e) {
			log.error("文件上传失败");
			return e.getMessage();
		}
	}
	/**
	 * 
	 * testDownload:(文件下载). <br/>
	 * @author wangzhch
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/testDownload",method = RequestMethod.POST)
	public void testDownload(@RequestParam("fileName") String fileName,HttpServletResponse response) {
		log.info("fileName:{}",fileName);
		String result = ioService.testDownload(response, fileName);
		log.info("result:{}",result);
	}
	public static void main(String[] args) {
		log.info(String.format("%1$tY-%1$tm-%1$te %1$tH:%1$tM:%1$tS", new Date()));
	}
}
